package com.trontria.simpledatamodel.demo;

import java.util.List;

import com.trontria.simpledatamodel.ModelData;
import com.trontria.simpledatamodel.ModelDataInterface;
import com.trontria.simpledatamodel.ModelManager;
import com.trontria.simpledatamodel.R;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {
	public static final String TAG = MainActivity.class.getSimpleName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ModelManager.manager().register(Poll.class);
		ModelManager.manager().config("test_db");
		ModelManager.manager().init(this, 1);
		
		Poll data = Poll.data();
		data.setId(1);
		data.setQuestion("What the hell????");
		ModelManager.manager().save(data);
		
		@SuppressWarnings("unchecked")
		List<Poll> pollList = (List<Poll>) ModelManager.manager().all(Poll.class);
		for (Poll poll : pollList) {
			Log.d(TAG, "Has: " + poll.getQuestion());
		}
		/**
		 * === How the code should be ===
		 * ModelManager.manager().register(Poll.model());
		 * ModelManager.manager().init();
		 * 
		 * == Select all ==
		 * List<ModelData> dataList = ModelManager.manager().all(Poll.model());
		 * 
		 * == Select ==
		 * WhereClause whereClause = new WhereClause();
		 * whereClause.addEqual(Poll.POLL_ID, 1);
		 * ModelData data = ModelManager.manager().select(whereClause);
		 * Poll.model().get(data, Poll.POLL_QUESTION);
		 * 
		 * == Insert ==
		 * ModelData data = Poll.model().emptyData();
		 * data.put(Poll.POLL_QUESTION, "What is this?");
		 * ModelManager.manager().save(data, Poll.model());
		 * 
		 * == Update ==
		 * WhereClause whereClause = new WhereClause();
		 * whereClause.addEqual(Poll.POLL_ID, 1);
		 * ModelData data = ModelManager.manager().select(whereClause);
		 * data.put(Poll.POLL_QUESTION, "What is this, again?");
		 * ModelManager.manager().save(data, Poll.model());
		 * 
		 * == JSON ==
		 * JSONObject object = Media.saveAs(data, Poll.model(), Media.MediaType.JSON);
		 * JSONArray array = Media.saveAs(dataList, Poll.model(), Media.MediaType.JSON);
		 * 
		 * Model model = Media.loadFrom(text, Poll.model(), Media.MediaType.JSON);
		 * Model model = Media.loadFrom(jsonObject, Poll.model(), Media.MediaType.JSON);
		 * List<Model> model = Media.loadFrom(jsonArray, Poll.model(), Media.MediaType.JSON);
		 * 
		 * == End ==
		 * ModelManager.manager().close();
		 */
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		ModelManager.manager().close();
		ModelManager.destroy();
	}
}